import os
from datetime import datetime

from flask import Flask, render_template, request, send_from_directory

from apps.zhengzhou_check.TaxiPassengerCheckInterface_ZhengZhou import main as ZhengZhouCheckMain

app = Flask(__name__)
app.add_url_rule('/templates/css/<path:filename>', endpoint='css_file',
                 view_func=lambda filename: send_from_directory('templates/css', filename))
app.add_url_rule('/templates/js/<path:filename>', endpoint='js_file',
                 view_func=lambda filename: send_from_directory('templates/js', filename))


app.add_url_rule('/data_cube/result/<path:filename>', endpoint='result_file',
                 view_func=lambda filename: send_from_directory('data_cube/result', filename))
@app.route("/")
def index():
    return render_template('index.html')


@app.route('/zhengzhou', methods=['GET', 'POST'])
def zhengzhou():
    request_method = request.method

    if request_method == 'GET':
        return render_template('zhengzhou_index.html')

    if request.method == 'POST':
        # 获取 POST 请求中的数据
        file = request.files.get('file')
        if file:
            # 可以对文件进行处理，比如保存文件
            file_name = f"zhengzhou_{datetime.now().date()}.csv"
            file.save('data_cube/uploads/' + file_name)
            ZhengZhouCheckMain(file_name)
            # time.sleep(5)
            return "success"
        else:
            return 'No file uploaded'
@app.route('/zhengzhou/process', methods=['GET', 'POST'])
def zhengzhou_process_result():
    request_method = request.method
    if request_method == 'GET':
        directory_path = './data_cube/result'
        files = os.listdir(directory_path)
        return render_template('zhengzhou_process_result.html', files_list = files)

if __name__ == '__main__':
    app.run()
